package com.mxpio.erp.inventory.policy.impl;

import com.mxpio.erp.common.enums.NoticeStatusEnums;
import com.mxpio.erp.common.enums.OrderEnums;
import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.inventory.entity.ToNotice;
import com.mxpio.erp.common.jpa.model.Order;
import com.mxpio.erp.common.order.policy.AbstractBizOrderPolicy;
import com.mxpio.erp.common.order.policy.OrderContext;
import com.mxpio.erp.common.vo.OrderExcuteLineVo;
import com.mxpioframework.jpa.JpaUtil;

import java.util.Date;
import java.util.List;

/**
 * 调拨单执行策略
 *
 */
public class DefaultToPolicy extends AbstractBizOrderPolicy {

	private List<OrderExcuteLineVo> orderExcuteVos;
	
	private String accessType;

	public DefaultToPolicy(List<OrderExcuteLineVo> orderExcuteVos, String accessType) {
		this.orderExcuteVos = orderExcuteVos;
		this.accessType = accessType;
	}

	@Override
	public void execute(OrderContext context) {
	}

	@Override
	public OrderContext config(OrderContext context) {
		context.setOrderExcuteVos(orderExcuteVos);
		context.setNoticeClass(ToNotice.class);
		context.setAccessType(accessType);
		if(OrderEnums.AccessType.IN.getCode().equals(accessType)){
			context.setPickingType(NoticeStatusEnums.NoticeType.TO_IN.getCode());
		}else{
			context.setPickingType(NoticeStatusEnums.NoticeType.TO_OUT.getCode());
		}
		return context;
	}

	@Override
	public boolean beforeExecute(OrderContext context) {
		return true;
	}

	@Override
	public void afterExecute(OrderContext context) {
		//更新状态
		Order order = context.getEntity();
		if(OrderStatusEnums.OrderStatus.ORDERED.getStatusCode().equals(order.getOrderStatus())) {
			order.setOrderStatus(OrderStatusEnums.OrderStatus.PROCESSING.getStatusCode());
			order.setExecuteTime(new Date());
			JpaUtil.update(order);
		}
	}

}
